package com.alog.yzd.bookmall.service.user;

import com.alog.yzd.bookmall.dao.user.UserDao;
import com.alog.yzd.bookmall.domain.user.User;
import com.alog.yzd.bookmall.exception.UserException;
import com.alog.yzd.bookmall.utils.mail.Mail;
import com.alog.yzd.bookmall.utils.mail.MailUtils;
import com.alog.yzd.commons.CommonUtils;


import javax.mail.MessagingException;
import javax.mail.Session;
import java.io.IOException;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Properties;

/**
 * 用户模块的业务层
 */
public class UserService {
    private UserDao userDao = new UserDao();

    public void updatePassword(String uid , String oldPwd, String newPwd) throws UserException {
        try {
            boolean bool = userDao.findByUidAndPassword(uid, oldPwd);
            if(!bool){
                throw new UserException("原始密码不正确");
            }
            userDao.updatePassword(uid, newPwd);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    /**
     * 登陆
     * @return
     */
    public User login(User user){
        try {
            User user2 = userDao.findByLoginnameAndLoginpass(user.getLoginname(), user.getLoginpass());
            return user2;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    /**
     * 激活用户
     * @param activationCode
     * @throws UserException
     */
    public void activation(String activationCode) throws UserException {
        User user = null;
        try {
            user = userDao.findByActivationCode(activationCode);
            if(user == null){
                throw new UserException("无效的激活码");
            }
            if(user.isStatus()){
                throw new UserException("已经激活过了, 请不要重复激活");
            }
            userDao.updateStatus(user.getUid(), true);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 校验用户名是否已经存在
     * @param loginname
     * @return
     */
    public boolean ajaxValidateLoginname(String loginname){
        try {
            return userDao.ajaxValidateLoginname(loginname);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 校验邮箱是否已经存在
     * @param email
     * @return
     */
    public boolean ajaxValidateEmail(String email){
        try {
            return userDao.ajaxValidateEmail(email);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 用户注册
     * @param user
     */
    public void regist(User user){
        //数据补齐
        user.setUid(CommonUtils.uuid());
        user.setStatus(false);
        user.setActivationCode(CommonUtils.uuid() + CommonUtils.uuid());
        //存入数据库
        try {
            userDao.addUser(user);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        //发邮件
        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getClassLoader().getResourceAsStream("email_template.properties"));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        String host = properties.getProperty("host");
        String name = properties.getProperty("username");
        String pass = properties.getProperty("password");
        Session session = MailUtils.createSession(host, name, pass);

        String from = properties.getProperty("from");
        String to = user.getEmail();
        String subject = properties.getProperty("subject");
        String content = MessageFormat.format(properties.getProperty("content"), user.getActivationCode());
        Mail mail = new Mail(from, to, subject, content);

        try {
            MailUtils.send(session, mail);
        } catch (MessagingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
